package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.domain.Clazz;
import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.dto.ClazzPageQueryDTO;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clazzs")
@Slf4j
public class ClazzController {
    @Autowired
    private ClazzService clazzService;
    /**
     * 添加班级信息
     * @param clazz
     * @return
     */
    @LogOperation
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        log.info("添加班级信息：{}",clazz);
        clazzService.addClazzInfo(clazz);
        return Result.success();
    }

    /**
     * 分页查询班级信息
     * @param clazzPageQueryDTO
     * @return
     */
    @GetMapping
    public Result list( ClazzPageQueryDTO clazzPageQueryDTO){

        PageResult list = clazzService.list(clazzPageQueryDTO);

        return Result.success(list);
    }
    /**
     * 班级信息修改
     * @param clazz
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("班级信息修改：{}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }
    /**
     * 查询所有班级
     * @param
     * @return
     */
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有班级");
        List<Clazz> clazzes = clazzService.listAll();
        return Result.success(clazzes);
    }

    /**
     * 根据id查询班级信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getClazzInfo(@PathVariable Integer id){
        log.info("查询班级信息：{}",id);
        Clazz clazz = clazzService.getClazzInfoById(id);
        return Result.success(clazz);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteClazz(@PathVariable Integer id){
        log.info("删除班级：{}",id);
        clazzService.deleteClazz(id);
        return Result.success();
    }

}
